package org.dandsoft.blackbox.knowledge;

import java.io.Serializable;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * User: dvilyuzhanin
 * Date: 13.01.2009 14:46:34
 */
public class BookInfo implements Serializable {
    private static final long serialVersionUID = -143583137588928943L;

    protected final static String VERSION_PARAMETER = "version";
    protected final static String NAME_PARAMETER = "name";
    protected final static String DESCRIPTION_PARAMETER = "parameter";

    private String bookName;
    private Version version;
    private String description;


    public void store(OutputStream out) throws IOException {
        Properties values = new Properties();

        if (getVersion() != null) {
            values.setProperty(VERSION_PARAMETER, getVersion().format());
        }

        if (getBookName() != null && getBookName().length() > 0) {
            values.setProperty(NAME_PARAMETER, getBookName());
        }

        if (getDescription() != null && getDescription().length() > 0) {
            values.setProperty(DESCRIPTION_PARAMETER, getDescription());
        }

        values.store(out, "");
    }

    public void load(InputStream in) throws IOException {
        Properties values = new Properties();
        values.load(in);

        final String versionStr = values.getProperty(VERSION_PARAMETER);
        if (versionStr != null && versionStr.length() > 0) {
            setVersion(Version.parse(versionStr));
        }

        final String bookNameStr = values.getProperty(NAME_PARAMETER);
        if (bookNameStr != null && bookNameStr.length() > 0) {
            setBookName(bookNameStr);
        }

        final String descriptionStr = values.getProperty(DESCRIPTION_PARAMETER);
        if (descriptionStr != null && descriptionStr.length() > 0) {
            setDescription(descriptionStr);
        }
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public Version getVersion() {
        return version;
    }

    public void setVersion(Version version) {
        this.version = version;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    public String toString() {
        return "BookInfo{" +
               "bookName='" + bookName + '\'' +
               ", version=" + version +
               ", description='" + description + '\'' +
               '}';
    }
}
